
import React, { useState, useEffect, useRef } from 'react'; // 这行必须写，不然报错，可能是eslint搞事情。
import {
  Row,
  Input,
  Form,
  DatePicker,
  Modal,
  message,
  TreeSelect,
  Radio,
  Select,
} from 'antd';
const { confirm } = Modal;
const { RangePicker } = DatePicker;
const { TreeNode } = TreeSelect;
const { Option } = Select;
const { TextArea } = Input;

const AddMenu = ({
  visible,
  onCancel,
  oninitList,
}) => {
  const [form] = Form.useForm();
  useResetFormOnCloseModal({
    form,
    visible,
  });

  //点击添加用户
  const onOk = () => {
    form.submit();
  };

  //表单提交方法
  const onFinish = values => {
    // resetUserPwd(userid, values.password).then(res => {
    //   if (res.code == 200) {
    //     console.log(res);
    //     message.success('修改成功');
    //     onhideUserModal();
    //     oninitList();
    //   } else if (res.code == 500) {
    //     message.warning(res.msg);
    //   }
    // });
  };

  return (
    <Modal
      title="修改密码"
      visible={visible}
      onOk={onOk}
      className="system-user-add-modal"
      onCancel={onCancel}
      cancelText="取消"
      okText="确定"
      style={{
        width: '300px !important',
        padding: '30px 20px',
        color: '#606266',
        fontSize: '14px',
        wordBreak: 'break-all',
      }}
    >
      <Form
        form={form}
        name="password"
        // labelAlign="right"
        // layout="horizontal"
        layout="vertical"
        className="addForm"
        onFinish={onFinish}
      >
        <Form.Item
          name="password"
          label="请输入新密码"
          rules={[{ required: true, message: '密码不能为空' }]}
        >
          <Input />
        </Form.Item>
      </Form>
    </Modal>
  );
};


const useResetFormOnCloseModal = ({ form, visible }) => {
  const prevVisibleRef = useRef();
  useEffect(() => {
    prevVisibleRef.current = visible;
  }, [visible]);
  const prevVisible = prevVisibleRef.current;
  useEffect(() => {
    if (!visible && prevVisible) {
      form.resetFields();
    }
  }, [visible]);
};

export default AddMenu;
